<?php
namespace app\home\controller;
// use app\index\
class Photo extends Base
{
    public function index()
    {      
        $id=request()->param('id');   
        $viewPage=$this->getIndexPage($id);    
        $photo=db("channel_photo");
        $channel=db("channel");
        $p=request()->param('p'); 
        if(!$p){
            $p=1;
        }
        $psize=20;        
        $stp=($p-1)*$psize;

        $fname=$channel->where("id",$id)->value("name");
        $pid=$channel->where("id=".$id)->value("pid");        
        if($pid > 0){
            $gcid=$pid;
            $pcid=$id;
            $gnavName=$channel->where("id=".$gcid)->value("name");
            // $lchannel=$this->get_left_channel($gcid);            
            $pnavName=$channel->where("id=".$pcid)->value("name");
        }else{
            $gcid=$id;          
            $gnavName=$channel->where("id=".$gcid)->value("name");
        }
        $where['cid']=$id; 
        $where['status']=1;
        $count=$photo->where($where)->count();
        $tolp=ceil($count/$psize);
        $mess=$photo->where($where)->order("is_top desc,sort asc , id DESC")->limit($stp,$psize)->select();               
        for ($i=0; $i < count($mess); $i++) { 
            $annexWh['status']=1;
            $annexWh['tid']=$mess[$i]['id'];
            $annexWh['tablename']="channel_cases";
            $mess[$i]['photos']=db("attachment")->where($annexWh)->limit(4)->select();
            $mess[$i]['photosCount']=db("attachment")->where($annexWh)->count();

            $mess[$i]['content']=html_entity_decode($mess[$i]['content']);
        }
        // 分页渲染
        $this->assign("p",$p);
        $this->assign("tolp",$tolp);
        $this->assign("count",$count); 
        // SEO META关键字配置
        $meta=$this->getSeoMeta($gnavName,$pnavName);
        $this->assign("meta",$meta);
        $modelx=$channel->where("id",$id)->value("modelx");
        $modelxMark=db("modelx")->where("id=".$modelx)->value("mark");
        $this->assign("modelxMark",$modelxMark);

        $banner=$this->get_banner($gcid);
        $this->assign("banner",$banner);
        $this->assign("pnavName",$pnavName);
        $this->assign("gnavName",$gnavName);
        $this->assign("lchannel",$lchannel);
        $this->assign("cid",$cid);
        $this->assign("gcid",$gcid);
        $this->assign("pcid",$pcid);
        $this->assign("fname",$fname);
        $this->assign("mess",$mess);
        return view($viewPage);
    }  
    public function detail()
    {
        $id=request()->param('id');       
        $photo=db("channel_photo");
        $channel=db("channel");
        $fname=$channel->where("id",$id)->value("name");
        $mess=$photo->find($id);
        $where['status']=1;
        $where['tid']=$mess['id'];
        $where['tablename']="channel_photo";
        // html_entity_decode 把HTML 实体转换回字符
        // htmlentities 把字符转换为 HTML 实体
        // htmlspecialchars 把预定义的字符转换为 HTML 实体
        $mess['content']=html_entity_decode($mess['content']);
        $mess['photos']=db("attachment")->where("tid=".$mess['id'])->select();
        // print_r($mess);exit;
        $viewPage=$this->getDetailPage($mess['cid']);
        $pid=$channel->where("id=".$mess['cid'])->value("pid");
        // print_r($pid);exit;
        if($pid > 0){
            $gcid=$pid;
            $pcid=$mess['cid'];
            $lchannel=$this->get_left_channel($pcid);   
            $gnavName=$channel->where("id=".$gcid)->value("name");         
            $pnavName=$channel->where("id=".$pcid)->value("name");
        }else{
            $gcid=$mess['cid'];         
            $gnavName=$channel->where("id=".$gcid)->value("name");
        } 
        $meta=$this->getSeoMeta($gnavName,$pnavName,$mess);
        $this->assign("meta",$meta);

        $modelx=$channel->where("id",$mess['cid'])->value("modelx");
        $modelxMark=db("modelx")->where("id=".$modelx)->value("mark");
        $this->assign("modelxMark",$modelxMark);
        $banner=$this->get_banner($gcid);
        $this->assign("banner",$banner);
        $this->assign("pnavName",$pnavName);
        $this->assign("gnavName",$gnavName);
        $this->assign("lchannel",$lchannel);
        $this->assign("pcid",$pcid);
        $this->assign("gcid",$gcid);
        $this->assign("fname",$fname);
        $this->assign("mess",$mess);
        return view($viewPage);
    }  
}
